package capitulo7;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class AnimationClassDemo extends JFrame implements Runnable{
	static int screenWidth=640;
	static int screenHeight=480;
	
	Thread gameLoop;
	Random rand = new Random();
	
	Graphics2D g2d;
	BufferedImage backBuffer;

	
	// Sprite Variables
	AnimatedSprite[] sprites = new AnimatedSprite[10];
	int totalSprites = 10;
	 
	public static void main(String[] args) {
		new AnimationClassDemo();
	}

	public AnimationClassDemo(){
		super("Animation Class Demo");
		setSize(screenWidth, screenHeight);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		backBuffer = new BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_RGB);
		g2d = backBuffer.createGraphics();
		
		// Create the new sprites (10)
		for (int i=0; i < totalSprites-1; i++){
			sprites[i] = new AnimatedSprite(this, g2d);
			sprites[i].load("explosion.png", 6, 5, 128, 128);
			sprites[i].position = new Point(rand.nextInt(screenWidth-128), rand.nextInt(screenHeight-128));
			sprites[i].frameDelay = 5;
			sprites[i].totalFrames = 30;
			sprites[i].velocity = new Point((int)rand.nextInt(2)-1,(int)rand.nextInt(2)-1);
			sprites[i].rotationRate = 1.0;
		}
		
		gameLoop = new Thread(this);
		gameLoop.start();
	}
		
	@Override
	public void run() {
		Thread t = Thread.currentThread();
		
		while (t == gameLoop){
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			gameUpdate();
		}
	}

	public void gameUpdate(){
		// draw the backgroup
		g2d.setColor(Color.BLACK);
		g2d.fill(new Rectangle(0, 0, screenWidth-1, screenHeight-1));
		
		// draw the sprites
		
		boolean collideThisInstance;
		for (int i=0; i < totalSprites-1; i++) {
			if (sprites[i]!=null){ 
				sprites[i].draw();
				sprites[i].drawBounds(Color.RED);
		
				// Keep the sprite in the screen boundary
				if (sprites[i].position.x < 0 || sprites[i].position.x > screenWidth-128)
					sprites[i].velocity.x *= -1;
				if (sprites[i].position.y < 0 || sprites[i].position.y > screenHeight-128)
					sprites[i].velocity.y *= -1;
			
				collideThisInstance = false;
				
				// Now we try to collide it		
				for (int n=0; n < totalSprites-1; n++){
					// We can't compare the sprite itself
					if (n==i) continue;			
			
					// We can't compare our current sprite with other sprite null
					if (sprites[n]!=null){
						if (sprites[i].getBounds().intersects(sprites[n].getBounds())){
							sprites[n] = null;
							collideThisInstance = true;
						}
					}
				}
				
				// if the current sprite collided with another, now it's null too 
				if (collideThisInstance) sprites[i] = null;
			}
		}
		
		/*
		g2d.setColor(Color.WHITE);
		g2d.drawString("Position:  " + sprite.position.x + ", " + sprite.position.y, 10, 40);
		g2d.drawString("Velocity:  " + sprite.velocity.x + ", " + sprite.velocity.y, 10, 60);
		g2d.drawString("Animation: " + sprite.currentFrame, 10, 80);*/
		
		repaint();
	}
	
	public void paint(Graphics g){
		g.drawImage(backBuffer, 0, 0, this);
	}
}
